<template>
  <div class="wsc-toast">
    <div class="wsc-toast-box" v-if="is_show">
      <div class="wsc-icon" v-show="type===3">
        <img class="wsc-icon-svg" src="./svg/loading.svg"/>
      </div>
      <div class="loading-text">{{message}}</div>
    </div>
    <div class="wsc-mask" v-show="is_show && mask"></div>
  </div>
</template>
<script>
export default {
  name: 'toast',
  data: function () {
    return {
      is_show: false,
      mask: false,
      type: 0,
      timer: null,
      message: '加载中...'
    }
  },
  methods: {
    show(options) {
      if (options === void 0) {
        options = {}
      }

      this.is_show = true

      if (typeof options === 'string') {
        options = {
          message: options,
          duration: 3000,
          forbidClick: true
        }
      }

      let defaultOptions = {
        message: '',
        type: 0, // 0 无图标 1 成功图标 2 失败图标 3 警告的图标
        mask: false, // 是否显示背景遮罩层
        duration: 3000, // 持续时间
        forbidClick: false // 是否禁止背景点击
      }

      for (var key in defaultOptions) {
        options[key] = options[key] !== undefined ? options[key] : defaultOptions[key]
      }

      this.message = options.message
      this.type = options.type
      this.mask = options.mask

      document.body.style.pointerEvents = options.forbidClick ? 'none' : 'auto'

      clearTimeout(this.timer)
      if (options.duration > 0) { // duration 等于0 则不会自动执行 hide()
        this.timer = setTimeout(() => {
          this.hide()
        }, options.duration)
      }
    },
    loading(options) {
      // console.log(options)
      this.show(options)
    },
    hide() {
      this.is_show = false
      this.type = 0
      this.mask = false
      document.body.style.pointerEvents = 'auto'
    },
    clear() {
      this.hide()
    }
  }
}
</script>
<style scoped>
.load-enter-to, .load-leave {
  opacity: 1;
}

.load-leave-active, .load-enter-active {
  transition: opacity .2s ease;
}

.load-enter, .load-leave-to {
  opacity: 0;
}

.wsc-toast-box {
  position: fixed;
  top: 50%;
  left: 50%;
  z-index: 1001;
  transform: translate3d(-50%, -50%, 0);
  min-width: 120px;
  /*height: 120px;*/
  background-color: rgba(0, 0, 0, .7);
  border-radius: 5px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  align-content: center
}

.wsc-icon {
  width: 60px;
  height: 60px;
  margin: 5px auto;
  font-size: 0;
}

.wsc-icon-svg {
  width: 100%;
  height: 100%;
}

.loading-text {
  font-size: 14px;
  line-height: 26px;
  padding: 0 5px;
  color: #fff;
}

.wsc-mask {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
}
</style>
